package com.epfresh.supplier.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.OrderAgentList;
import com.epfresh.supplier.entity.OrderOnGroupList;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import multitype.MultiTypeAdapter;

/**
 * Created by zzq on 2016/7/19.
 */
public class OrderOnGroupAdapter extends BaseAdapter implements View.OnClickListener {

    public ListView lv;
    public Context context;
    public List<OrderOnGroupList> list;
    private AdapterView.OnItemClickListener onItemClickListener;

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OrderOnGroupAdapter(Context context, ListView lv, List<OrderOnGroupList> groupList) {
        this.context = context;
        this.list = groupList;
        this.lv = lv;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int groupPosition, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_order_on_group, null, false);
            vh.ll_item_order_group = convertView.findViewById(R.id.ll_item_order_group);
            vh.tv_item_order_on_group_id = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_id);
            vh.tv_item_order_on_group_from = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_from);
            vh.tv_item_order_on_group_to = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_to);
            vh.tv_item_order_on_group_state = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_state);
            vh.tv_item_order_on_group_date = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_date);
            vh.iv_item_order_on_group_goods_state = (ImageView) convertView.findViewById(R.id.iv_item_order_on_group_goods_state);

            vh.iv_item_order_on_group_product = (ImageView) convertView.findViewById(R.id.iv_item_order_on_group_product);
            vh.tv_item_order_on_group_product_title = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_product_title);
            vh.tv_item_order_on_group_product_price = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_product_price);
            vh.tv_item_order_on_group_product_size_detail = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_product_size_detail);
            vh.tv_item_order_on_group_product_package = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_product_package);
            vh.tv_item_order_on_group_product_size_already = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_product_size_already);

            vh.tv_item_order_on_group_money_goods = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_money_goods);
            vh.tv_item_order_on_group_money_freight = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_money_freight);
            vh.tv_item_order_on_group_money_all = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_money_all);


            vh.tv_item_order_on_group_goods_code = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_goods_code);

            vh.tv_item_order_on_group_minus = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus);
            vh.tv_item_order_on_group_post_now = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_post_now);
            vh.tv_item_order_on_group_post_pre = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_post_pre);
            vh.tv_item_order_on_group_sure = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_sure);
            vh.tv_item_order_on_group_cancel = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_cancel);
            vh.tv_item_order_on_group_pay_remind = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_pay_remind);
            vh.tv_item_order_on_group_pay_pre = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_pay_pre);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.ll_item_order_group.setTag(R.id.item_key_position,groupPosition);
        vh.ll_item_order_group.setOnClickListener(this);
        vh.tv_item_order_on_group_minus.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_minus.setOnClickListener(this);
        vh.tv_item_order_on_group_post_now.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_post_now.setOnClickListener(this);
        vh.tv_item_order_on_group_post_pre.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_post_pre.setOnClickListener(this);
        vh.tv_item_order_on_group_sure.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_sure.setOnClickListener(this);
        vh.tv_item_order_on_group_cancel.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_cancel.setOnClickListener(this);
        vh.tv_item_order_on_group_pay_remind.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_pay_remind.setOnClickListener(this);
        vh.tv_item_order_on_group_pay_pre.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_pay_pre.setOnClickListener(this);

        vh.tv_item_order_on_group_goods_code.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_goods_code.setOnClickListener(this);

        OrderOnGroupList order = list.get(groupPosition);
        vh.tv_item_order_on_group_id.setText("单号："+order.getOrderNo());
        vh.tv_item_order_on_group_from.setText(order.getBaseCityName());
        vh.tv_item_order_on_group_to.setText(order.getAgencyCityName());
        vh.tv_item_order_on_group_date.setText("预计"+order.getDeliverTime()+"发货");
        ImageLoader.getInstance().displayImage(order.getProductImg(), vh.iv_item_order_on_group_product);
        vh.tv_item_order_on_group_state.setText(order.getOrderStatusTitle());
//        if("CANDELIVERY".equals(order.getWatermark())){
//            vh.iv_item_order_on_group_goods_state.setImageResource(R.mipmap.on_group_full);
//            vh.iv_item_order_on_group_goods_state.setVisibility(View.VISIBLE);
//        }else if("NOREACHQTY".equals(order.getWatermark())){
//            vh.iv_item_order_on_group_goods_state.setImageResource(R.mipmap.on_group_no_full);
//            vh.iv_item_order_on_group_goods_state.setVisibility(View.VISIBLE);
//        }else {
//            vh.iv_item_order_on_group_goods_state.setVisibility(View.GONE);
//        }


        vh.tv_item_order_on_group_product_price.setText(order.getProductPriceUnit()+"元/"+order.getChargeUnit());
        vh.tv_item_order_on_group_product_title.setText(order.getProductTitle());
        vh.tv_item_order_on_group_product_package.setText(order.getPack());
        vh.tv_item_order_on_group_product_size_detail.setText("满"+order.getCountLower()+order.getChargeUnit()+"发货，可拼"+order.getCountUppe()+order.getChargeUnit());
        vh.tv_item_order_on_group_product_size_already.setText("已拼"+order.getSumProductCount()+order.getChargeUnit()+"(还剩"+order.getReMainProductCount()+order.getChargeUnit()+")");

        vh.tv_item_order_on_group_money_goods.setText("货款："+order.getProductPriceUnit()+"元*"+order.getProductCount()+order.getChargeUnit()+"="+order.getProductPrice()+"元");
        vh.tv_item_order_on_group_money_freight.setText("运费："+order.getRequiredFreight()+"元");
        vh.tv_item_order_on_group_money_all.setText(order.getRequiredPayTotle()+"元");
        initButton(order,vh);


        convertView.setTag(vh);
        convertView.setClickable(true);
        return convertView;
    }

    public class ViewHolder {
        View ll_item_order_group;

        TextView tv_item_order_on_group_id;

        TextView tv_item_order_on_group_from, tv_item_order_on_group_to;
        TextView tv_item_order_on_group_state;
        TextView tv_item_order_on_group_date;
        ImageView iv_item_order_on_group_goods_state;

        ImageView iv_item_order_on_group_product;
        TextView tv_item_order_on_group_product_title;
        TextView tv_item_order_on_group_product_price;
        TextView tv_item_order_on_group_product_size_detail;
        TextView tv_item_order_on_group_product_package;
        TextView tv_item_order_on_group_product_size_already;

        TextView tv_item_order_on_group_money_goods;
        TextView tv_item_order_on_group_money_freight;
        TextView tv_item_order_on_group_money_all;

        TextView tv_item_order_on_group_goods_code;
        TextView tv_item_order_on_group_minus,tv_item_order_on_group_post_now, tv_item_order_on_group_post_pre,
                tv_item_order_on_group_sure,tv_item_order_on_group_cancel,tv_item_order_on_group_pay_remind,tv_item_order_on_group_pay_pre;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
// 交货码，查看物流，取消订单，确认收货，支付， 详情
            if(v.getTag(R.id.item_key_status)!=null && v.getTag(R.id.item_key_status) instanceof Long) {
                onItemClickListener.onItemClick(null, v, position, (Long) v.getTag(R.id.item_key_status));
            }
            else {
                onItemClickListener.onItemClick(null, v, position, position);
            }
        }
    }
    void initButton(OrderOnGroupList order,ViewHolder vh){
        vh.tv_item_order_on_group_minus.setVisibility(View.GONE);
        vh.tv_item_order_on_group_post_now.setVisibility(View.GONE);
        vh.tv_item_order_on_group_post_pre.setVisibility(View.GONE);
        vh.tv_item_order_on_group_sure.setVisibility(View.GONE);
        vh.tv_item_order_on_group_cancel.setVisibility(View.GONE);
        vh.tv_item_order_on_group_pay_remind.setVisibility(View.GONE);
        vh.tv_item_order_on_group_pay_pre.setVisibility(View.GONE);
        vh.tv_item_order_on_group_goods_code.setVisibility(View.GONE);

        for (OrderOnGroupList.ButtonListBean bean:order.getButtonList()) {
            switch (bean.getCode()){
                case "supplyPayFirst":
                    //Log.e("timeFlag",bean.getReminderTime()+"_____");
                    //vh.tv_item_order_on_group_pay_pre.setTag(R.id.item_key_status, bean.getNowRemindTime());
                    if("".equals(bean.getReminderTime())){
                        vh.tv_item_order_on_group_pay_pre.setVisibility(View.GONE);
                        //timerStop();
                        //isTimerAlive = false;
                    }else {
                        vh.tv_item_order_on_group_pay_pre.setText(bean.getTitle()+"\n"+bean.getReminderTime());
                        vh.tv_item_order_on_group_pay_pre.setVisibility(View.VISIBLE);
                    }
                    break;
                case "supplyCancelOrder":
                    vh.tv_item_order_on_group_cancel.setVisibility(View.VISIBLE);
                    break;
                case "supplyPayLast":
                    //Log.e("timeFlag",bean.getReminderTime()+"_____");
                    vh.tv_item_order_on_group_pay_remind.setVisibility(View.VISIBLE);
                    if("".equals(bean.getReminderTime())){
                        vh.tv_item_order_on_group_pay_remind.setText("支付尾款");
                    }else{
                        vh.tv_item_order_on_group_pay_remind.setTag(R.id.item_key_status,bean.getNowRemindTime());
                        vh.tv_item_order_on_group_pay_remind.setText(bean.getTitle()+"\n"+bean.getReminderTime());
                    }
                    break;
                case "supplyPubPre":
                    vh.tv_item_order_on_group_post_pre.setVisibility(View.VISIBLE);
                    break;
                case "supplyPubNow":
                    vh.tv_item_order_on_group_post_now.setVisibility(View.VISIBLE);
                    break;
                case "supplyApplyFefund":
                    vh.tv_item_order_on_group_minus.setVisibility(View.VISIBLE);
                    break;
                case "supplyViewInspectCode":
                    vh.tv_item_order_on_group_goods_code.setVisibility(View.VISIBLE);
                    break;
                case "supplyCheckGoods":
                    vh.tv_item_order_on_group_sure.setText(bean.getTitle());
                    vh.tv_item_order_on_group_sure.setVisibility(View.VISIBLE);
                    break;
            }

        }

    }


    public Timer timer;
    boolean isTimerAlive;
    TimerTask task ;
    Handler handler = new Handler(){

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    notifyDataSetChanged();
                    break;
            }
            super.handleMessage(msg);
        }

    };

    public void timerStart() {
        timerStop();
        if(timer == null)
            timer = new Timer();

        task =  new TimerTask() {
            @Override
            public void run() {
                try {


                    if (isTimerAlive){
                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        isTimerAlive = true;

        timer.schedule(task, 0, 1000);
    }


    public void timerStop() {
        isTimerAlive = false;
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (task != null){
            task.cancel();
            task = null;
        }
    }


}
